package communicationMessages;

import java.util.Map.Entry;
import java.util.logging.Level;
import logger.LoggerWriter;
import data.Forum;
import data.Member;
import data.MemberDataBase;
import data.SubForum;
import database.DB;

public class MsgCountMessage extends Message { //return the total msgs the user sent

	@Override
	public String getAnswer() {
		String []arr = this.content.split(delimiter);
		LoggerWriter logger = LoggerWriter.getLoggerWriter();
		if (arr.length != 2)
		{
			logger.log("failed"+"MsgCountMessage wrong format"+delimiter, Level.WARNING);
			return "failed"+delimiter+"MsgCountMessage wrong format"+delimiter;
		}
		String answer = "";
		String userName = arr[0];//operate
		String selectedUser = arr[1];
		String userRoll = DB.getUserRoll(userName);
		boolean isAdmin = DB.isAdmin(userName);
		if (!isAdmin)
		{
			logger.log("not an admin try get num of msgs bu user", Level.WARNING);
			return "failed" + delimiter + "not an admin try get num of msgs by user" + delimiter;
		}
		else
		{
			int counter =0;
//get number of messages per userSelected
			counter = DB.getMessagePerUserSelected(selectedUser);
			if (counter != 0)
			{
				answer = "msgsCount" + delimiter + userRoll + delimiter + counter + delimiter;
				
			}
			else
			{
				answer = "msgsCount" + delimiter + userRoll + delimiter + 0 + delimiter;
			}
		}
		System.out.println("MsgCountMessage\n**********\n " +answer);
		return answer;
	}

}
/*
@Override
public String getAnswer() {
	String []arr = this.content.split(delimiter);
	LoggerWriter logger = LoggerWriter.getLoggerWriter();
	if (arr.length != 2)
	{
		logger.log("failed"+"MsgCountMessage wrong format"+delimiter, Level.WARNING);
		return "failed"+delimiter+"MsgCountMessage wrong format"+delimiter;
	}
	String answer = "";
	String userName = arr[0];
	String selectedUser = arr[1];
	String userRoll = DB.g(userName);
	MemberDataBase membersDataBase = MemberDataBase.getMembersDataBase();
	Member member = membersDataBase.getMember(userName);
	boolean isAdmin = member.get_isAdmin();
	if (!isAdmin)
	{
		logger.log("not an admin try get num of msgs bu user", Level.WARNING);
		return "failed" + delimiter + "not an admin try get num of msgs by user" + delimiter;
	}
	else
	{
		Forum forum = Forum.getForum();
		int counter =0;
		for (Entry<Integer, SubForum> entry : forum.get_subForumColection().entrySet()) {
			counter += entry.getValue().getNumberOfMessagesToUser(selectedUser);
		}
		if (counter != 0)
		{
			ans = "msgsCount" + delimiter + userRoll + delimiter + counter + delimiter;
			
		}
		else
		{
			ans = "msgsCount" + delimiter + userRoll + delimiter + 0 + delimiter;
		}
	}
	System.out.println(ans);
	return ans;
}

}*/